<template>
  <div class="app-container">
    <el-card shadow="never">
      <div class="search-container">
        <el-form :inline="true" :model="queryParams">
          <#list table.columns as column>
          <el-form-item label="${column.comment}">
            <el-input v-model="queryParams.${CodeGeneratorUtils.toCamelCase(column.name, false)}" placeholder="请输入${column.comment}" clearable />
          </el-form-item>
          </#list>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <div class="operation-container">
        <el-button type="primary" icon="el-icon-plus" @click="handleCreate">新增</el-button>
      </div>

      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%"
        size="medium">
        <#list table.columns as column>
        <el-table-column prop="${CodeGeneratorUtils.toCamelCase(column.name, false)}" label="${column.comment}" />
        </#list>
        <el-table-column label="操作" width="200" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              style="color: #F56C6C"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="fetchData"
      />
    </el-card>

    <!-- 表单弹窗 -->
    <${entityName}Form
      :visible="formDialog.visible"
      :data="formDialog.data"
      :mode="formDialog.mode"
      @success="handleFormSuccess"
      @close="formDialog.visible = false"
    />
  </div>
</template>

<script>
import { page${entityName}, delete${entityName} } from '@/api/${lowEntityName}'
import ${entityName}Form from './components/Form'

export default {
  name: '${entityName}List',
  components: { ${entityName}Form },
  data() {
    return {
      loading: false,
      tableData: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        <#list table.columns as column>
        ${CodeGeneratorUtils.toCamelCase(column.name, false)}: '',
        </#list>
      },
      formDialog: {
        visible: false,
        mode: 'create',
        data: null
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.loading = true
      page${entityName}(this.queryParams)
        .then(response => {
          this.tableData = response.records
          this.total = response.total
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleSearch() {
      this.queryParams.pageNum = 1
      this.fetchData()
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        <#list table.columns as column>
        ${CodeGeneratorUtils.toCamelCase(column.name, false)}: '',
        </#list>
      }
      this.fetchData()
    },
    handleCreate() {
      this.formDialog = {
        visible: true,
        mode: 'create',
        data: null
      }
    },
    handleEdit(row) {
      this.formDialog = {
        visible: true,
        mode: 'edit',
        data: { ...row }
      }
    },
    handleDelete(row) {
      this.$confirm(`确定删除<#noparse>$</#noparse>{row.id}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delete${entityName}(row.id).then(() => {
          this.<#noparse>$</#noparse>message.success('删除成功')
          this.fetchData()
        })
      })
    },
    handleFormSuccess() {
      this.formDialog.visible = false
      this.fetchData()
    }
  }
}
</script>

<style scoped>
.search-container {
  margin-bottom: 15px;
}
.operation-container {
  margin-bottom: 15px;
}
</style>